﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CCF20Model
{
    public static class StringExtension
    {
        public static string FormatSMS(this string crashTemplate,string idc)
        {

            ModelFactory modelFactory = new ModelFactory();
            Device device = ((DeviceModel)modelFactory.CreateModel(ModelName.DeviceModel)).GetDeviceByIDC(idc);

            if (device != null && device.Vehicle != null )
            {
                return FormatSMS(crashTemplate, device.Vehicle);
            }
            else
            {
                string failReason = string.Format("{0}-{1}", device == null ? "device is null" : string.Empty,
                                                             device.Vehicle == null ? "vehicle is null" : string.Empty);
                return string.Format("无法给idc {0}发送短信 原因 {1}", idc,failReason);
            }
        }
        public static string FormatSMS(this string crashTemplate, Customer customer)
        {
            if (customer.Vehicles.Count > 0)
            {
                return FormatSMS(crashTemplate, customer.Vehicles.First(), customer);
            }
            else
            {
                return string.Format("无法给客户 {0}发送短信 原因 无法找到对应车辆", customer.Name);
            }
        }
        public static string FormatSMS(this string crashTemplate, Vehicle vehicle)
        {
            return FormatSMS(crashTemplate, vehicle, vehicle.Customer);
        }
        public static string FormatSMS(this string crashTemplate, Vehicle vehicle, Customer customer)
        {
            CompanyProfile profile = Utility.GetCompanyProfile();
            if (profile == null)
            {

                throw new Exception("Company Profile is Empty. Can't send out sms");
            }
            string strFault = string.Empty;
            foreach (Fault fault in vehicle.Faults.Where(f => f.IsActive && f.IsConfirmed))
            {
                strFault = string.Format("{0},{1}",strFault, fault.CodeDescription);
            }

            string strMaintenance = string.Empty;
            foreach (VehicleMaintenanceHistory history in vehicle.VehicleMaintenanceHistories.Where(h => h.IsActive))
            {
                strMaintenance = string.Format("{0},{1}", strMaintenance, history.MaintenanceType.Name);
            }

            return crashTemplate.Replace("#lxr#", customer.Name)
                                        .Replace("#NowLs#", vehicle.CurrentMileage.ToString())
                                        .Replace("#CompanyName#", profile.CompanyName)
                                        .Replace("#CompanyAddress#", profile.CompanyAddress)
                                        .Replace("#CompanyPhone#", profile.Telephone)
                                        .Replace("#ServerPer#", profile.Contact)
                                        .Replace("#oc#", strFault)
                                        .Replace("#ByXmName#", strMaintenance);
        }
    }
}
